import 'package:card_topia/common_provider/user_provider.dart';
import 'package:card_topia/common_widget/my_app_bar.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/my_router.dart';
import 'package:card_topia/utils/dialog_util.dart';
import 'package:card_topia/utils/event_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


///设置页面

class SettingPage extends StatelessWidget {
  const SettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustomAppBar(
        title: "设置",
        showLeading: true,
      ),
      body: SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        child: Column(
          children: [
          //账号
            SettingContainer(title: "账号",children: [
              SettingItem(title: "编辑个人资料",content: "头像,昵称等", onTap: (){},),
              SettingItem(title: "账号安全", onTap: (){},),
            ],),
            //通知
            SettingContainer(title: "通知",children: [
              SettingItem(title: "推送设置", onTap: (){},),
              SettingItem(title: "互动消息设置", onTap: (){},),
            ],),
            //系统功能
            SettingContainer(title: "系统功能",children: [
              SettingItem(title: "深色模式", onTap: (){},),
              SettingItem(title: "隐私设置", onTap: (){},),
              SettingItem(title: "清除缓存", onTap: (){},),
            ],),
            //关于
            SettingContainer(title: "关于",children: [
              SettingItem(title: "用户反馈", onTap: (){},),
              SettingItem(title: "用户协议", onTap: (){},),
              SettingItem(title: "隐私政策", onTap: (){},),
              SettingItem(title: "个人信息共享清单", onTap: (){},),
              SettingItem(title: "联系我们", onTap: (){},),
              SettingItem(title: "注销账号", onTap: (){},),
              SettingItem(title: "版本信息", content: "v1.2.0",showTrailing: false,),
            ],),
            //退出登录
            Consumer(builder: (context,ref,child)=>InkWell(
              onTap: ()async{
                DialogUtil.showDefaultDialog(context,title: "提示",content: "退出后将保留用户信息到云端,不会删除用户信息",confirm: "退出登录",onConfirm: (){
                  ref.read(userInfoProvider).clearUserInfo();
                  // context.pop();
                  MyRouter.pop();
                  EventUtil.eventBus.fire(TabChangeEvent(0));
                });
              },
              child: Container(
                margin: EdgeInsets.symmetric(horizontal: 16.w,vertical: 30.w),
                decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                height: 48.w,
                alignment: Alignment.center,
                child: Text("退出登录",style: TextStyle(fontFamily: harmony,color: Colors.white,fontSize: 16.sp),),
              ),
            ))
          ],
        ),
      ),
    );
  }
}

//设置子模块
class SettingContainer extends StatelessWidget {
  const SettingContainer({super.key, this.title, required this.children});
  final String? title;
  final List<Widget> children;
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w,vertical: 16.w),
      child: Column(
        children: [
          if(title!=null)
            Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 12.w),
                child: Text(title??"",style: TextStyle(fontSize: 14.sp,fontFamily: harmony,color: descColor),)),
          SizedBox(height: 8.w,),
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8.w)
            ),
            padding: EdgeInsets.symmetric(horizontal: 12.w,vertical: 8.w),
            child: Column(
              children: children,
            ),
          )
        ],
      ),
    );
  }
}


//设置子模块item
class SettingItem extends StatelessWidget {
  const SettingItem({super.key, required this.title, this.onTap, this.content, this.showTrailing = true, this.icon});
  final String title;
  final Function? onTap;
  final String? content;
  final bool showTrailing;
  final Widget? icon;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){
        if(onTap!=null){
          onTap!();
        }
      },
      child: SizedBox(
        height: 42.w,
        child: Row(
          children: [
            if(icon!=null)
              icon!,
            if(icon!=null)
            SizedBox(width: 8.w,),
            Text(title,style: TextStyle(fontSize: 14.sp,fontFamily: harmony,color: textColor,),),
            const Spacer(),
            Text(content??"",style: TextStyle(fontSize: 14.sp,color: descColor),),
            SizedBox(width: 4.w,),
            if(showTrailing)
              const Icon(Icons.keyboard_arrow_right,color: textColor,)
          ],
        ),
      ),
    );
  }
}
